from typing import Callable
from Board import Board
from Frame import Frame
from TemplateSolver import TemplateSolver
from GameManager import GameManger


class SimpleSolver(TemplateSolver):
    def __init__(self) -> None:
        super().__init__(self.solve_func)

    def solve(self, frame: Frame):
        return super().solve(frame)

    def solve_func(frame: Frame, board: Board):
        steps = frame.current_board.get_available_step()

        # if len(steps == 60):
        #     dir
        for step in steps:
            dir = step[0]
            x = step[1]
            y = step[2]
            for i in range(5):
                for j in range(5):
                    if board.box[i][j] != 0:
                        continue
                    # 1.三条边
                    elif board.line[0][i][j] == 0 and board.line[0][i + 1][j] != 0 and board.line[1][j][i] != 0 and board.line[1][j + 1][i] != 0:
                        dir = 0
                        x = i
                        y = j
                        score = 100
                    elif board.line[0][i][j] != 0 and board.line[0][i + 1][j] == 0 and board.line[1][j][i] != 0 and board.line[1][j + 1][i] != 0:
                        dir = 0
                        x = i + 1
                        y = j
                        score = 100
                    elif board.line[0][i][j] != 0 and board.line[0][i + 1][j] != 0 and board.line[1][j][i] == 0 and board.line[1][j + 1][i] != 0:
                        dir = 1
                        x = j
                        y = i
                        score = 100
                    elif board.line[0][i][j] != 0 and board.line[0][i + 1][j] != 0 and board.line[1][j][i] != 0 and board.line[1][j + 1][i] == 0:
                        dir = 1
                        x = j + 1
                        y = i
                        score = 100
            # 2.一条边
            if score < 100:
                for i in range(5):
                    for j in range(5):
                        if board.line[0][i][j] != 0 and board.line[0][i + 1][j] == 0 and board.line[1][j][i] == 0 and board.line[1][j + 1][i] == 0:
                            dir = 0
                            x = i + 1
                            y = j
                            score = 80
                        elif board.line[0][i][j] == 0 and board.line[0][i + 1][j] != 0 and board.line[1][j][i] == 0 and board.line[1][j + 1][i] == 0:
                            dir = 0
                            x = i
                            y = j
                            score = 80
                        elif board.line[0][i][j] == 0 and board.line[0][i + 1][j] == 0 and board.line[1][j][i] != 0 and board.line[1][j + 1][i] == 0:
                            dir = 1
                            x = j + 1
                            y = i
                            score = 80
                        elif board.line[0][i][j] == 0 and board.line[0][i + 1][j] == 0 and board.line[1][j][i] == 0 and board.line[1][j + 1][i] != 0:
                            dir = 1
                            x = j
                            y = i
                            score = 80
            # 3.无边
            if score < 80:
                for i in range(5):
                    for j in range(5):
                        if board.line[0][i][j] == 0 and board.line[0][i + 1][j] == 0 and board.line[1][j][i] == 0 and board.line[1][j + 1][i] == 0:
                            dir = 0
                            x = i + 1
                            y = j
                            score = 60
            # 4.两条边
            if score < 60:
                for i in range(5):
                    for j in range(5):
                        if board.line[0][i][j] == 0:
                            dir = 0
                            x = i
                            y = j
                        elif board.line[0][i + 1][j] == 0:
                            dir = 0
                            x = i + 1
                            y = j
                        elif board.line[1][j][i] == 0:
                            dir = 1
                            x = j
                            y = i
                        elif board.line[1][j + 1][i] == 0:
                            dir = 1
                            x = j + 1
                            y = i
        return dir, x, y
